package com.wgh.search.controller;

import com.wgh.search.entity.ProductDoc;
import com.wgh.search.service.ProductSearchService;
import com.wgh.common.vo.PageResult;
import com.wgh.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 搜索控制器
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    
    @Autowired
    private ProductSearchService productSearchService;
    
    /**
     * 商品搜索接口
     * @param keyword 搜索关键词
     * @param categoryId 分类ID
     * @param brandId 品牌ID
     * @param priceRange 价格范围
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param filters 其他过滤条件
     * @return 搜索结果
     */
    @GetMapping("/products")
    public Result<PageResult<ProductDoc>> searchProducts(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long brandId,
            @RequestParam(required = false) String priceRange,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) Map<String, Object> filters) {
        
        PageResult<ProductDoc> result = productSearchService.searchProducts(
                keyword, categoryId, brandId, priceRange, sortField, sortOrder, pageNum, pageSize, filters);
        
        return Result.success(result);
    }
    
    /**
     * 获取搜索建议
     * @param keyword 搜索关键词
     * @param count 建议数量
     * @return 搜索建议列表
     */
    @GetMapping("/suggestions")
    public Result<?> getSearchSuggestions(
            @RequestParam String keyword,
            @RequestParam(required = false, defaultValue = "10") Integer count) {
        
        return Result.success(productSearchService.getSearchSuggestions(keyword, count));
    }
    
    /**
     * 获取搜索聚合信息
     * @param keyword 搜索关键词
     * @return 聚合结果
     */
    @GetMapping("/aggregations")
    public Result<?> getSearchAggregations(
            @RequestParam(required = false) String keyword) {
        
        return Result.success(productSearchService.getSearchAggregations(keyword));
    }
    
    /**
     * 根据商品ID获取商品详情
     * @param productId 商品ID
     * @return 商品文档
     */
    @GetMapping("/products/{productId}")
    public Result<?> getProductDetail(@PathVariable Long productId) {
        
        ProductDoc productDoc = productSearchService.getProductFromIndex(productId);
        if (productDoc == null) {
            return Result.error("商品不存在");
        }
        
        return Result.success(productDoc);
    }
    
    /**
     * 重建商品索引
     * 注意：此接口应只允许管理员访问
     * @return 操作结果
     */
    @PostMapping("/rebuild-index")
    public Result<?> rebuildProductIndex() {
        
        boolean result = productSearchService.rebuildProductIndex();
        if (result) {
            return Result.success("索引重建成功");
        } else {
            return Result.error("索引重建失败");
        }
    }
}